﻿using JRCore.protocol;
using MCInsertDb.DAL;
using MCInsertDb.Model;
using System;
using System.Configuration;
using System.IO;
using System.ServiceProcess;

namespace MCInsertDb
{
    public partial class Service1 : ServiceBase
    {
        public string gFTPfolder = null; 
        public string gIPAddress = null;
        public Service1()
        {
            InitializeComponent();
            Init();
        }

        protected override void OnStart(string[] args)
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞上传数据库服务启动");

        }

        protected override void OnStop()
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞上传数据库服务停止");

        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
	            gFTPfolder = ConfigurationManager.AppSettings["FTPfolder"];
                gIPAddress = ConfigurationManager.AppSettings["IPAddress"];

            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
            if (!Directory.Exists(gFTPfolder)) Directory.CreateDirectory(gFTPfolder);
            fileSystemWatcher1.Path = gFTPfolder;
        }
        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            string filename = System.IO.Path.GetFileNameWithoutExtension(e.FullPath);//文件名  
            string[] s = filename.Split(new char[] { '_' });

            DataRecvRecord record = new DataRecvRecord();
            FullDataRecvRecord(record, s, e);
            InsertDb(record);
        }


        /// <summary>
        /// 入库函数
        /// </summary>
        /// <param name="record">杰瑞数据定义</param>
        #region InsertDb入库函数
        private void InsertDb(DataRecvRecord record)
        {
            TGS_DATA tgs_data = new TGS_DATA();

            tgs_data.Id = Guid.NewGuid().ToString("N");
            tgs_data.Cartype = record.P21;//询问
            tgs_data.Carnum = record.P23;
            tgs_data.Passdate = DateTime.ParseExact(record.P19, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            tgs_data.Addresscode = record.P11;
            tgs_data.Departmentcode = record.P12;
            tgs_data.Datasource = record.P7;
            tgs_data.Devicecode = record.P8;
            tgs_data.Lanenum = record.P17;
            tgs_data.Carspeed = 0;
            tgs_data.Pic1 = record.P37;
            tgs_data.Pic2 = record.P41;

            switch (record.P16)
            {
                case "2":
                    tgs_data.Dirid = "3";
                    break;
                case "3":
                    tgs_data.Dirid = "2";
                    break;
                default:
                    tgs_data.Dirid = record.P16;
                    break;

            }
            tgs_data.Typecolor = record.P22;
            tgs_data.Deviceid = record.P8;
            string tableNameArray = record.P19.Substring(4,2);
            String tableName = "TGS_DATA_HIS" + tableNameArray;
            try
            {
                if (new TGS_DATADAL().Insert(tgs_data, tableName) > 0)
                {
                    string DBInfo = tgs_data.Passdate + " " + tgs_data.Carnum + " 入库成功";
                    LogHelper.WriteLog(typeof(Service1), DBInfo);
                                      
                }
            }
            catch (System.Exception ex)
            {
                string DBInfo = tgs_data.Passdate + " " + tgs_data.Carnum + " 入库失败";

                LogHelper.WriteLog(typeof(Service1), ex, DBInfo);
            }
        }
        #endregion


        /// <summary>
        /// 处理字符串
        /// </summary>
        /// <param name="record"></param>
        /// <param name="s"></param>
        /// <param name="e"></param>
        #region FullDataRecvRecord处理字符串
        private void FullDataRecvRecord(DataRecvRecord record, string[] s, FileSystemEventArgs e)
        {
            record.P11 = s[0];
            record.P16 = s[1];
            record.P12 = s[1];
            record.P7 = "1";
            record.P8 = s[1];

            record.P17 = s[2].Substring(1);
            if (s[4].Contains("无车牌"))
            {
                record.P22 = "";
                record.P23 = "";
            }
            else
            {
                record.P22 = s[5];
                record.P23 = s[4];
            }
            switch (s[6])
            {
                case "大型车":
                    record.P21 = "01";
                    break;
                case "小型车":
                    record.P21 = "02";
                    break;
                default:
                    record.P21 = "01";
                    break;
            }
            //record.P24 = "0";
            record.P5 = "0";
            //record.P13 = s[11];

            //record.P8 = s[11];
            
            record.P19 = s[3].Substring(0, 14);
            string ftpName = e.FullPath.Substring(gFTPfolder.Length);
            record.P37 = (gIPAddress + ftpName).Replace(@"\", @"/");

        }
        #endregion
    }
}
